package event

type ProcFunc func(*Event)
type SimpleProcessor struct {
	f             ProcFunc
	isMiddleware  bool
	procType      int
	parent        EventProcessor
	name          string
	childrenTypes []int
}

func NewSimpleMiddleware(name string, f ProcFunc) *SimpleProcessor {
	p := NewSimpleProcessor(0, name, f)
	p.isMiddleware = true
	return p
}

func NewSimpleProcessor(procType int, name string, f ProcFunc) *SimpleProcessor {
	return &SimpleProcessor{
		name:          name,
		procType:      procType,
		f:             f,
		isMiddleware:  false,
		childrenTypes: []int{},
	}
}

func (f *SimpleProcessor) Name() string {
	return f.name
}

// whether is a middle ware, not implemented for a certain type
func (f *SimpleProcessor) IsMiddleware() bool {
	return f.isMiddleware
}

// the specific event type to process, if IsMiddleware() is true, the Type() is useless
func (f *SimpleProcessor) Type() int {
	return f.procType
}

// process function
func (f *SimpleProcessor) Process(e *Event) {
	f.f(e)
}

// parent processor
func (f *SimpleProcessor) Parent() EventProcessor {
	return f.parent
}

func (f *SimpleProcessor) ChildrenEventTypes() []int {
	return f.childrenTypes
}

func (f *SimpleProcessor) SetChildrenEventTypes(types []int) {
	if types == nil {
		return
	}
	// filter duplicated types
	typesMap := make(map[int]struct{})
	for _, t := range types {
		typesMap[t] = struct{}{}
	}
	types1 := make([]int, 0, len(typesMap))
	for t := range typesMap {
		types1 = append(types1, t)
	}
	f.childrenTypes = types1
}
